/*
 * isa_dma.c
 *
 * Copyright (C) 2019 Aleksandar Andrejevic <theflash@sdf.lonestar.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <isa_dma.h>
#include <lock.h>

static DECLARE_LOCK(dma_lock);

sysret_t isa_dma_alloc(word_t size, area_t *buffer)
{
    if (!size) return ERR_INVALID;

    memory_acquire_area(MIN_PHYS_ADDR_BITS,
                        23,
                        PAGE_ALIGN_UP((size_t)size),
                        /* Choose an alignment so that it doesn't cross a 64K boundary */
                        1 << (32 - __builtin_clz(PAGE_ALIGN_UP((unsigned int)size))),
                        buffer);
    return is_area_valid(buffer) ? ERR_SUCCESS : ERR_NOMEMORY;
}

void isa_dma_free(const area_t *buffer)
{
    memory_release_area(buffer);
}

void isa_dma_read(dword_t channel, const area_t *buffer, word_t count)
{
    channel &= 7;
    ASSERT(count <= buffer->count * PAGE_SIZE);
    if (count == 0) return;
    else count--;

    uintptr_t address = buffer->pages->number * PAGE_SIZE;
    lock_acquire(&dma_lock);
    cpu_write_port_byte(ISA_DMA_SINGLE_MASK_REG(channel), ISA_DMA_MASK_ON | (channel & 3));

    cpu_write_port_byte(ISA_DMA_FF_RESET_REG(channel), 0xFF);
    cpu_write_port_byte(ISA_DMA_SAR(channel), address & 0xFF);
    cpu_write_port_byte(ISA_DMA_SAR(channel), (address >> 8) & 0xFF);
    cpu_write_port_byte(ISA_DMA_PAR(channel), (address >> 16) & 0xFF);

    cpu_write_port_byte(ISA_DMA_FF_RESET_REG(channel), 0xFF);
    cpu_write_port_byte(ISA_DMA_CNT(channel), count & 0xFF);
    cpu_write_port_byte(ISA_DMA_CNT(channel), count >> 8);

    cpu_write_port_byte(ISA_DMA_MODE_REG(channel), 0x48 | (channel & 3));

    cpu_write_port_byte(ISA_DMA_SINGLE_MASK_REG(channel), (channel & 3));
    lock_release(&dma_lock);
}

void isa_dma_write(dword_t channel, const area_t *buffer, word_t count)
{
    channel &= 7;
    ASSERT(count <= buffer->count * PAGE_SIZE);
    if (count == 0) return;
    else count--;

    uintptr_t address = buffer->pages->number * PAGE_SIZE;
    lock_acquire(&dma_lock);
    cpu_write_port_byte(ISA_DMA_SINGLE_MASK_REG(channel), ISA_DMA_MASK_ON | (channel & 3));

    cpu_write_port_byte(ISA_DMA_FF_RESET_REG(channel), 0xFF);
    cpu_write_port_byte(ISA_DMA_SAR(channel), address & 0xFF);
    cpu_write_port_byte(ISA_DMA_SAR(channel), (address >> 8) & 0xFF);
    cpu_write_port_byte(ISA_DMA_PAR(channel), (address >> 16) & 0xFF);

    cpu_write_port_byte(ISA_DMA_FF_RESET_REG(channel), 0xFF);
    cpu_write_port_byte(ISA_DMA_CNT(channel), count & 0xFF);
    cpu_write_port_byte(ISA_DMA_CNT(channel), count >> 8);

    cpu_write_port_byte(ISA_DMA_MODE_REG(channel), 0x44 | (channel & 3));

    cpu_write_port_byte(ISA_DMA_SINGLE_MASK_REG(channel), (channel & 3));
    lock_release(&dma_lock);
}
